<?php

/*****************************************************************************
 * 
 *  Copyright 2009 Sylvain Dudoit (contact : sylvain.dudoit at gmail.com)
 *  
 *  This file is part of Poupitchi.
 *
 *  Poupitchi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Poupitchi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Poupitchi.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// Include db

require_once("db.".P_DB.".php");

///////////////////////////////////////////////////////////////////////////////
// Check form submition

if (isset($_FILES)) {
  foreach($_FILES as $upload) {
    $upload['login'] = $_POST['login'];
    addPhoto($upload);
  }
}
if (isset($_GET['delete'])) {
  removePhoto($_GET['delete']);
}

///////////////////////////////////////////////////////////////////////////////
// Define list

$start   = isset($_GET['start']) ? $_GET['start'] : 0;
$total   = countPhotos();
$nbitems = P_NB_PHOTOS;

$photos  = getPhotos($nbitems, $_GET['start']);

///////////////////////////////////////////////////////////////////////////////
// Print page

?>

<div id="bloc_photos">
	<div id="link_form">
		<a href="#bloc_form">&gt; <?php etr("Add photo") ?> &lt;</a>
	</div>
	<?php foreach($photos as $photo) : ?>
	  <?php ePhoto($photo['file']); ?>
	  <?php if ($P_ADMIN) : ?>
	    <h4> 
	    		<a href="index.php?action=show&module=photos&delete=<?php e($photo['file']); ?>"
	    			 onclick="return confirm('<?php etr("Are you sure you want to delete ?"); ?>')">
	    				[ <?php etr("Delete"); ?> ] 
	    		</a>
	    </h4>
	  <?php endif; ?>
	<?php endforeach;?>
	<?php ePagination($start, $total, $nbitems); ?>	
	<div id="bloc_form">
		<h2>&gt; <?php etr("Add photo") ?> &lt;</h2>
	  <form action="index.php?action=show&module=photos" method="post" enctype="multipart/form-data">
	    <input type="hidden" name="login" value="<?php e($P_USER); ?>"/>
	    <table>
	      <?php for ($i=0; $i<5; $i++) :?>
        <tr>
          <th><?php etr("File")?></th>
          <td><input type="file" class="title" name="upload_<?php e($i)?>" size="50" /></td>
        </tr>
        <?php endfor;?>
	      <tr>
	        <th>&nbsp;</th>
	        <td><input type="submit" class="submit" value="<?php etr("Upload")?>"/></td>
	      </tr>
	    </table>
	  </form>
	</div>
</div>
